﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Bookvar.ObjectModel.Collections
{
    public class TopicPositionCollection : ObservableKeyedCollection<Guid, TopicPosition>
    {
        protected override Guid GetKeyForItem( TopicPosition item )
        {
            return item.ID;
        }

        public void UpdateWithRange( IEnumerable<TopicPosition> newPositions )
        {
            foreach ( var newPosition in newPositions )
            {
                TopicPosition position;
                if ( !TryGetItem( newPosition.ID, out position ) )
                {
                    Add( newPosition );
                }
                else
                {
                    position.X = newPosition.X;
                    position.Y = newPosition.Y;
                    position.IsMoved = newPosition.IsMoved;
                }
            }
        }

        public TopicPositionCollection Clone()
        {
            var cloning = new TopicPositionCollection();
            cloning.AddRange( from position in this select position.Clone() );
            return cloning;
        }
    }
}